
package lsy.Servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lsy.Dao.DaoFactory;
import lsy.entity.Student;
import lsy.tools.PageInfo;
import lsy.tools.PathUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/student")
public class StudentServlet extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method = request.getParameter("method");
        if ("list".equals(method)) {
            this.list(request, response);
        } else if ("add".equals(method)){
            this.add(request, response);
        }else if ("edit".equals(method)){
            this.findById(request,response);
        }else if ("editsubmit".equals(method)){
            this.editsubmit(request,response);
        }else if ("delete".equals(method)){
            this.delete(request,response);
        }

    }

    private void list(HttpServletRequest request, HttpServletResponse response) {
        //当前页码
        Integer pageNo = getIntParameter(request, "pageNo");
        Integer studentId = getIntParameter(request, "studentId");
        String stuName = request.getParameter("stuName");
        String stuNum = request.getParameter("stuNum");

        Student student = new Student();
        student.setstudentId(studentId);
        student.setstuName(stuName);
        student.setstuNum(stuNum);

        //构造了一个pageInfo对象
        PageInfo <Student> pageInfo = new PageInfo<>(pageNo);
        try {
            pageInfo = DaoFactory.getInstance().getStudentDao().list(student,pageInfo);
        } catch (SQLException e1) {
            e1.printStackTrace();
        }
        try {
            request.setAttribute("pageInfo", pageInfo);
            //回写到页面
            request.setAttribute("student", student);
            request.getRequestDispatcher("student/list.jsp").forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void add(HttpServletRequest request, HttpServletResponse response){
        Integer studentId= Integer.valueOf(request.getParameter("studentId"));
        String stuNum=request.getParameter("stuNum");
        String stuName=request.getParameter("stuName");
        String pawd=request.getParameter("pawd");
        Student student=new Student();
        student.setstudentId(studentId);
        student.setstuNum(stuNum);
        student.setstuName(stuName);
        student.setpawd(pawd);
        //重定向到列表页面
        try {
            DaoFactory.getInstance().getStudentDao().add(student);
            response.sendRedirect(PathUtils.getBasePath(request)+"student?method=list");
        } catch (SQLException e) {
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }

    }

    private void findById(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");
        try {
            Student student = DaoFactory.getInstance().getStudentDao().findById(Integer.parseInt(id));
            request.setAttribute("student", student);
            request.getRequestDispatcher("student/update.jsp").forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private void editsubmit(HttpServletRequest request, HttpServletResponse response) {
        Integer studentId = Integer.valueOf(request.getParameter("studentId"));
        String stuNum = request.getParameter("stuNum");
        String stuName = request.getParameter("stuName");
        Student student = new Student();
        student.setstuName(stuName);
        student.setstuNum(stuNum);
        student.setstudentId(studentId);
        try {
            DaoFactory.getInstance().getStudentDao().update(student);
            //直接重定向到列表页面
            response.sendRedirect(PathUtils.getBasePath(request)+"student?method=list");
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void delete(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");
        try {
            DaoFactory.getInstance().getStudentDao().delete(Integer.parseInt(id));
            //直接重定向到列表页面
            response.sendRedirect(PathUtils.getBasePath(request)+"student?method=list");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public Integer getIntParameter(HttpServletRequest request,String name) {
        if(StringUtils.isNoneBlank(request.getParameter(name))) {
            return Integer.parseInt(request.getParameter(name));
        }else {
            return null;
        }
    }
}
